package com.example.administrator.student4;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import org.json.JSONException;

import java.io.IOException;

/**
 * Created by Administrator on 2016/11/30.
 */
public class SearchPersonAndTerm extends Fragment {
    EditText et_name;
    Spinner sp_term;
    Button search;
    String name,term;
    MyHandler myHandler;
    String [] arr_term = {"term one","term two"},arrname;
    View view;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.searchbypersonandterm,container,false);
        uiinit();
        myHandler = new MyHandler();
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name = et_name.getText().toString();
                term = sp_term.getSelectedItem().toString();
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        try {
                            arrname = WebUtil.namefind(name);
                            myHandler.sendEmptyMessage(0x111);
                        } catch (IOException | JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();

            }
        });
        return view;
    }
    public void uiinit(){
        sp_term = (Spinner) view.findViewById(R.id.term);
        et_name = (EditText) view.findViewById(R.id.name);
        search = (Button) view.findViewById(R.id.search);
        ArrayAdapter<String> adapter_term = new ArrayAdapter<>(getActivity(),android.R.layout.simple_expandable_list_item_1,arr_term);
        sp_term.setAdapter(adapter_term);
    }
    class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 0x111){
                if(arrname.length>1) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setTitle("选择名字").setSingleChoiceItems(arrname, 0, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            name = arrname[which];
                        }
                    }).setNegativeButton("取消", null).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(getActivity(),SearchByPerson.class);
                            intent.putExtra("name",name);
                            intent.putExtra("term",term);
                            intent.putExtra("becon",2);
                            startActivity(intent);
                        }
                    }).show();
                }
                else {
                    Intent intent = new Intent(getActivity(),SearchByPerson.class);
                    intent.putExtra("name",name);
                    intent.putExtra("term",term);
                    intent.putExtra("becon",2);
                    startActivity(intent);
                }
            }
        }
    }
}
